package tech.lq0.mineapex;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.DamageSource;
import net.minecraft.util.IndirectEntityDamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import tech.lq0.mineapex.armor.EvoShield;
import tech.lq0.mineapex.registry.ItemRegistry;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class EventHandler {
    @SubscribeEvent
    public static void onDamage(LivingDamageEvent event) {
        DamageSource source = event.getSource();
        Entity attacker;
        if (source instanceof IndirectEntityDamageSource) {
            // 间接伤害获取真来源
            IndirectEntityDamageSource indirectSource = (IndirectEntityDamageSource) source;
            attacker = indirectSource.getTrueSource();
            if (attacker == null) attacker = event.getSource().getImmediateSource();
        } else {
            attacker = event.getSource().getImmediateSource();
        }

        Entity target = event.getEntity();

        // 玩家对实体造成伤害
        if (attacker instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) attacker;
            ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
            if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
                if (armor.getStack().getChildTag("evo") == null) {
                    CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                    nbt.putFloat("max", EvoShield.shield[0]);
                    nbt.putFloat("current", EvoShield.shield[0]);
                    nbt.putFloat("damage", 0);
                }
                // 获取进化护盾相关数据
                CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");

                int oldLevel = EvoShield.getLevel(armor);

                // 如果是进化甲
                if (!nbt.getBoolean("noevo")) {
                    // 累加造成伤害
                    float damage = nbt.getFloat("damage");
                    damage += event.getAmount();
                    nbt.putFloat("damage", damage);
                }
                // 设置护盾承伤上限
                int newLevel = EvoShield.getLevel(armor);
                if (oldLevel != newLevel) {
                    nbt.putFloat("max", EvoShield.shield[newLevel]);
                    // 播放护甲升级音效
                    player.getEntityWorld().playSound(player.getPosX(), player.getPosY(), player.getPosZ(), SoundEvents.ENTITY_ENDER_DRAGON_GROWL, SoundCategory.PLAYERS, 1.0F, 1.0F, true);
                }

                // 测试用：造成伤害时增加相应护盾值
                //nbt.putFloat("current", Math.min(shield, nbt.getFloat("current") + event.getAmount()));
            }
        }

        // 玩家受到伤害
        if (target instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) target;
            ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
            if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
                if (armor.getStack().getChildTag("evo") == null) {
                    CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                    nbt.putFloat("max", EvoShield.shield[0]);
                    nbt.putFloat("current", EvoShield.shield[0]);
                    nbt.putFloat("damage", 0);
                }
                CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                float current = nbt.getFloat("current");
                float dmg = event.getAmount();
                if (current >= dmg) {
                    event.setAmount(0);
                    // 有护盾时受伤
                    player.getEntityWorld().playSound(player.getPosX(), player.getPosY(), player.getPosZ(), SoundEvents.ITEM_SHIELD_BLOCK, SoundCategory.PLAYERS, 1, 0, true);
                    current -= dmg;
                } else {
                    // 播放碎甲音效
                    event.setAmount(event.getAmount() - current);
                    current = 0;
                }
                nbt.putFloat("current", current);
            }
        }
    }

    @SubscribeEvent
    public static void onPlayerDeath(LivingDeathEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entity;
            ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.CHEST);
            if (!armor.isEmpty() && armor.getItem().equals(ItemRegistry.EVO_SHIED.get())) {
                CompoundNBT nbt = armor.getStack().getOrCreateChildTag("evo");
                if (nbt.getBoolean("noevo")) {
                    nbt.putFloat("current", nbt.getFloat("max"));
                } else {
                    nbt.putFloat("max", EvoShield.shield[0]);
                    nbt.putFloat("current", EvoShield.shield[0]);
                    nbt.putFloat("damage", 0);
                }
            }
        }
    }
}


